package com.dahua.receive;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.widget.Toast;

import com.dahua.db.DownloadInfo;


@SuppressLint("NewApi")
public class CompleteReceiver extends BroadcastReceiver {

    private DownloadManager downloadManager;
    private Service service;

    public CompleteReceiver(Service service) {
        this.service = service;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            Toast.makeText(context, "The download is complete", Toast.LENGTH_LONG).show();
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            // 判断这个id与之前的id是否相等，如果相等说明是之前的那个要下载的文件
            DownloadInfo di = new DownloadInfo(context);
            long savedId = di.loadData().getId();
            if (id != savedId) {
                return;
            }
            di.setDownloading(false).saveData();
            Query query = new Query();
            query.setFilterById(id);
            downloadManager = (DownloadManager) context
                    .getSystemService(Context.DOWNLOAD_SERVICE);
            Cursor cursor = downloadManager.query(query);

            int columnCount = cursor.getColumnCount();
            String path = null;
            // 这里把所有的列都打印一下，有什么需求，就怎么处理,文件的本地路径就是path
            while (cursor.moveToNext()) {
                for (int j = 0; j < columnCount; j++) {
                    String columnName = cursor.getColumnName(j);
                    System.out.print("columnName: " + columnName + "    ");
                    String string = cursor.getString(j);
                    System.out.println("columnValue: " + string);
                    if (columnName.equals("local_filename")) {
                        path = string;
                        break;
                    }
                }
            }
            cursor.close();
            if (path != null) {
//				System.out.println(columnName + ": " + string);
                installAPK(context, Uri.fromFile(new File(path)));
            } else {
//				System.out.println(columnName + ": null");
            }
            // 如果sdcard不可用时下载下来的文件，那么这里将是一个内容提供者的路径，这里打印出来，有什么需求就怎么样处理
			/*cursor = context.getContentResolver().query(Uri.parse(uri), null,
					null, null, null);
			columnCount = cursor.getColumnCount();
			while (cursor.moveToNext()) {
				for (int j = 0; j < columnCount; j++) {
					String columnName = cursor.getColumnName(j);
					String string = cursor.getString(j);
					if (string != null) {
						System.out.println(columnName + ": " + string);
					} else {
						System.out.println(columnName + ": null");
					}
				}
			}*/
            cursor.close();
            service.stopSelf();
        }
    }

    private void installAPK(Context context, Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

}
